package com.glorystone.ins.controller;

import com.glorystone.ins.common.JsonResult;
import com.glorystone.ins.common.common.DataTable;
import com.glorystone.ins.common.common.PageVo;
import com.glorystone.ins.domain.CreditTotal;
import com.glorystone.ins.domain.DepositRecord;
import com.glorystone.ins.domain.Member;
import com.glorystone.ins.interceptor.PageList;
import com.glorystone.ins.service.CreditRecordService;
import com.glorystone.ins.service.CreditTotalService;
import com.glorystone.ins.service.DepositRecordService;
import com.glorystone.ins.service.MemberService;
import com.glorystone.ins.util.DictUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 授信信息
 * Created by wangwei on 2018/3/15.
 */
@Controller
@RequestMapping("/credit")
public class CreditController extends BaseController {


    @Autowired
    private DepositRecordService depositRecordService;
    @Autowired
    private CreditRecordService creditRecordService;
    @Autowired
    private CreditTotalService creditTotalService;
    @Autowired
    private MemberService memberService;


    /**
     * 查询授信历史
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/record/queryTableData", method = RequestMethod.GET)
    @ResponseBody
    public DataTable<CreditTotal> queryCreditTableData(HttpServletRequest request) {
        PageVo vo = parametToPageVo(request, CreditTotal.class);
        PageList<CreditTotal> list = creditTotalService.findHistoryList(vo);
        DataTable data = resultToDataTable(list);
        return data;
    }


    /**
     * 查询保证金历史
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/deposit/queryTableData", method = RequestMethod.GET)
    @ResponseBody
    public DataTable<DepositRecord> queryDepositTableData(HttpServletRequest request) {
        PageVo vo = parametToPageVo(request, DepositRecord.class);
        PageList<DepositRecord> list = depositRecordService.findHistoryList(vo);
        DataTable data = resultToDataTable(list);
        return data;
    }


    @ResponseBody
    @RequestMapping(value = "/calc/{creditAmount}", method = RequestMethod.GET)
    public ResponseEntity<BigDecimal> calcDepositExpectAmount(@PathVariable("creditAmount") BigDecimal creditAmount) {
        String rate = DictUtils.getDictValue("1", "deposit_rate", String.valueOf(10));
        BigDecimal amount;
        if (Strings.isNullOrEmpty(rate) || creditAmount == null) {
            amount = BigDecimal.ZERO;
        } else {
            amount = creditAmount.multiply(BigDecimal.valueOf(10000)).multiply(new BigDecimal(rate)).divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_UP);
        }


        return new ResponseEntity<>(amount, HttpStatus.OK);


    }

    @ResponseBody
    @RequestMapping(value = "/last/{channelId}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> findLastPhaseCredit(@PathVariable("channelId") Long channelId) {

        CreditTotal creditTotal = creditTotalService.findPrePhase(channelId);

        JsonResult result;
        if (creditTotal == null) {
            result = JsonResult.createErrorMsg("不存在上期");
        } else {
            List<Member> memberList = memberService.findByChannleId(channelId);
            if (CollectionUtils.isNotEmpty(memberList)) {
                memberList = memberList.stream().filter( i -> i.getType() == Member.TypeEnum.ACTUAL_CONTROLLER.getValue() || i.getType() == Member.TypeEnum.SENIOR_MANAGER.getValue()).collect(Collectors.toList());
            }
            Map<String, Object> map = Maps.newHashMap();
            map.put("credit", creditTotal);
            map.put("memberList", memberList);

            result = JsonResult.createSuccess(map);

        }
        return new ResponseEntity<>(result, HttpStatus.OK);

    }

}
